package com.chancingpack.util;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.generic.DateTool;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Skeyedu on 2016/11/17.
 */
public class VelocityUtil {
    private static DateTool dateTool = new DateTool();
    private static VelocityConfigurer velocityConfigurer;

    public VelocityConfigurer getVelocityConfigurer() {
        return velocityConfigurer;
    }

    public void setVelocityConfigurer(VelocityConfigurer velocityConfigurer) {
        VelocityUtil.velocityConfigurer = velocityConfigurer;
    }

    public static VelocityEngine getConfigurer() {
        return velocityConfigurer.getVelocityEngine();
    }

    public static DateTool getDateTool() {
        return dateTool;
    }

    public static String getHtmlByMap(String vmPath, Map<String, Object> map, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {

        return VelocityEngineUtils.mergeTemplateIntoString(velocityConfigurer.getVelocityEngine(), vmPath, "utf-8", map);
    }
}
